package com.gmail.avereshchaga.db.dao;

import com.gmail.avereshchaga.db.entity.IItem;
import com.gmail.avereshchaga.db.entity.ItemImpl;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
@Repository
public class ItemDAOImpl implements IItemDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void updateItem(IItem item) {
        sessionFactory.getCurrentSession().update(item);
    }

    @Override
    public void deleteItem(Long id) {
        Session session = sessionFactory.getCurrentSession();
        Criteria crit = session.createCriteria(ItemImpl.class);
        crit.add(Restrictions.eq("id", id));
        IItem item = (ItemImpl) crit.uniqueResult();
        sessionFactory.getCurrentSession().delete(item);
    }

    @Override
    public List<IItem> findAll(Integer page) {
        Session session = sessionFactory.getCurrentSession();
        Criteria crit = session.createCriteria(ItemImpl.class);
        crit.setFirstResult(page * 100).setMaxResults(100);
        List<IItem> items = crit.list();
        return items;
    }

    @Override
    public IItem findById(Long id) {
        Session session = sessionFactory.getCurrentSession();
        Criteria crit = session.createCriteria(ItemImpl.class);
        crit.add(Restrictions.eq("id", id));
        IItem item = (ItemImpl) crit.uniqueResult();
        return item;
    }

    @Override
    public List<IItem> findByFilter(String code, String name, Date date, String username, Integer page) {
        String codeStrQ = code.equals("") ? "" : " and (item.code =:code)";
        String nameStrQ = name.equals("") ? "" : " and (lower(item.name) =:name)";
        String dateStrQ = date == null ? "" : " and (item.createdAt =:date)";
        Query q = sessionFactory.getCurrentSession()
                .createQuery("from ItemImpl item where "
                + "(lower(item.user.firstName) like :username or lower(item.user.lastName) like :username"
                + " or ((lower(item.user.firstName)  || ' ' || lower(item.user.lastName)) like :username)"
                + " or ((lower(item.user.lastName)  || ' ' || lower(item.user.firstName)) like :username))"
                + codeStrQ + dateStrQ + nameStrQ
                + " order by item.id asc");

        if (!code.equals("")) {
            q.setString("code", code.trim());
        }
        if (!name.equals("")) {
            q.setString("name", name.trim().toLowerCase());
        }
        if (date != null) {
            q.setDate("date", date);
        }
        q.setString("username", username.trim().toLowerCase() + "%");
        q.setMaxResults(100);
        q.setFirstResult(page * 100);
        List<IItem> items = q.list();
        return items;
    }
}
